//
//  CSMyOrderHeadV.m
//  ProjectforHDX
//
//  Created by wusiping on 16/8/8.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "CSMyOrderHeadV.h"
#import "CMyOrderCenterDetailCheck.h"
#import "DeliveryModel.h"
#define   HEADER_HEIGHT 70
#define   GAP 5
#define   LEFT_GAP 15
#define DEL_FontSize 13
#define DEL_LABEL_HEIGHT 21


@implementation CSMyOrderHeadV

-(id)initHeaderViewWithOrderStatusDic:(NSDictionary*)orderStatusDic AndReceiverDict:(NSDictionary *)receiverDict AndlogisticsDict:(NSDictionary *)logisticsDcit AndCustomerServiceDict:(NSDictionary *)customerServiceDict{
    self=[super initWithFrame:CGRectMake(0, 0, BOUNDSWIDTH, HEADER_HEIGHT * 2)];
    self.backgroundColor = [UIColor whiteColor];
    
    self.orderStatusDic = [[NSMutableDictionary alloc]initWithDictionary:orderStatusDic];
    self.receiverDict = [[NSMutableDictionary alloc]initWithDictionary:receiverDict];
    self.logisticsDcit = [[NSMutableDictionary alloc]initWithDictionary:logisticsDcit];
    self.customerServiceDict = [[NSMutableDictionary alloc]initWithDictionary:customerServiceDict];
    [self UIConfig];
    return self;
}

- (void)UIConfig{
    
    //订单UI
    UIView *orderView = [[UIView alloc]init];//WithFrame:CGRectMake(0, 0, self.width, HEADER_HEIGHT)];
    if ([[self.receiverDict objectForKey:@"arriveTime"] isEqualToString:@""]) {
        [orderView setFrame:CGRectMake(0, 0, self.width, HEADER_HEIGHT)];
    }else if ([[self.orderStatusDic objectForKey:@"service_time"] isEqualToString:@""]) {
        [orderView setFrame:CGRectMake(0, 0, self.width, 75)];
    }else {
        [orderView setFrame:CGRectMake(0, 0, self.width, 90)];
    }

    
    orderView.backgroundColor = SYSTEM_COLOR;
    //订单号
    RZCustomLable *orderCodeTitleLabel=[[RZCustomLable alloc] init];
    [orderCodeTitleLabel setFont:[UIFont systemFontOfSize:12]];
    [orderCodeTitleLabel setTextColor:[UIColor whiteColor] ];
    [orderView addSubview:orderCodeTitleLabel];
    orderCodeTitleLabel.text = [NSString stringWithFormat:@"%@：%@",[THMangerTool language:@"main.orderCode"],[self.orderStatusDic objectForKey:@"orderCode"]];
    [orderCodeTitleLabel setFrame:CGRectMake(LEFT_GAP, 15, self.width - LEFT_GAP, 21)];
    [orderCodeTitleLabel sizeToFit];

    //下单时间
    RZCustomLable *orderTimeLable = [[RZCustomLable alloc]init];
    [orderTimeLable setFont:[UIFont systemFontOfSize:12]];
    [orderTimeLable setTextColor:[UIColor whiteColor] ];
    [orderView addSubview:orderTimeLable];
    if ([[self.orderStatusDic objectForKey:@"customerOrderTime"] isEqualToString:@""]) {
        orderTimeLable.text = [NSString stringWithFormat:@"%@：%@",[THMangerTool language:@"order.time"],[self.orderStatusDic objectForKey:@"orderTime"]];
    }else {
        orderTimeLable.text = [NSString stringWithFormat:@"%@：%@",[THMangerTool language:@"order.time"],[self.orderStatusDic objectForKey:@"customerOrderTime"]];
    }
    [orderTimeLable setFrame:CGRectMake(LEFT_GAP, orderCodeTitleLabel.bottom + GAP, self.width-15, 21)];
    [orderTimeLable sizeToFit];
    
    //服务开始时间
    RZCustomLable *service_timeLable = [[RZCustomLable alloc]init];
    [service_timeLable setFont:[UIFont systemFontOfSize:12]];
    [service_timeLable setTextColor:[UIColor whiteColor] ];
    if ([[self.orderStatusDic objectForKey:@"service_time"] isEqualToString:@""]) {
    }else {
        [orderView addSubview:service_timeLable];
    }
    service_timeLable.text = [NSString stringWithFormat:@"%@：%@",[THMangerTool language:@"order.service.time"],[self.orderStatusDic objectForKey:@"service_time"]];
    [service_timeLable setFrame:CGRectMake(LEFT_GAP, orderTimeLable.bottom + GAP, self.width-15, 21)];
    [service_timeLable sizeToFit];
    
    //希望送达时间
    RZCustomLable *customerOrderTimeLabel = [[RZCustomLable alloc]init];
    [customerOrderTimeLabel setFont:[UIFont systemFontOfSize:12]];
    [customerOrderTimeLabel setTextColor:[UIColor whiteColor] ];
    if ([[self.receiverDict objectForKey:@"arriveTime"] isEqualToString:@""]) {
    }else {
        [orderView addSubview:customerOrderTimeLabel];
    }
    customerOrderTimeLabel.text = [NSString stringWithFormat:@"%@：%@",[THMangerTool language:@"order.wish.arrive.time"],[self.receiverDict objectForKey:@"arriveTime"]];
    if ([[self.orderStatusDic objectForKey:@"service_time"] isEqualToString:@""]) {
        [customerOrderTimeLabel setFrame:CGRectMake(LEFT_GAP, orderTimeLable.bottom + GAP, self.width-15, 21)];
    }else {
        [customerOrderTimeLabel setFrame:CGRectMake(LEFT_GAP, service_timeLable.bottom + GAP, self.width-15, 21)];
    }
    [customerOrderTimeLabel sizeToFit];
    
    [self addSubview:orderView];
    
    //订单状态
    RZCustomLable *statusLab = [[RZCustomLable alloc]init];
    [statusLab setTextAlignment:NSTextAlignmentCenter];
    [statusLab setTextColor:[UIColor whiteColor]];
    [statusLab setFrame:CGRectMake(self.width - 70, 5, 50, 50)];
    [statusLab setText:[self.orderStatusDic objectForKey:@"statusName"]];
    NSInteger type = [[self.orderStatusDic objectForKey:@"status"] integerValue];
    
    // 黄色：253，186，69
    // 橘色: 253,158,88
    // 蓝色：77，170，271
    // 绿色：69，202，145
    
    UIColor *orderStatusColorYellow = [UtilCheck  getRZColor:253 green:186 blue:69 alpha:1];
    UIColor *orderStatusColorOrange = [UtilCheck  getRZColor:253 green:158 blue:88 alpha:1];
    UIColor *orderStatusColorBlue = [UtilCheck  getRZColor:77 green:170 blue:271 alpha:1];
    UIColor *orderStatusColorGreen = [UtilCheck  getRZColor:69 green:202 blue:145 alpha:1];
    
    switch (type) {
        case 0://已取消
            [statusLab setBackgroundColor:[UIColor redColor]];
            break;
        case 2://待收货
            [statusLab setBackgroundColor:orderStatusColorBlue];
            break;

        case 3://仓库待发货
            [statusLab setBackgroundColor:orderStatusColorOrange];
            break;

        case 4://待安排发货
            [statusLab setBackgroundColor:orderStatusColorYellow];
            break;

        case 5://待确认
            [statusLab setBackgroundColor:orderStatusColorOrange];
            break;

        case 13://按时送达
            [statusLab setBackgroundColor:orderStatusColorGreen];
            break;

        case 14://延时送达
            [statusLab setBackgroundColor:orderStatusColorGreen];
            break;

            
        default:
            [statusLab setBackgroundColor:orderStatusColorYellow];
            break;
    }
    
    
    
    
    statusLab.layer.cornerRadius = 25;
    statusLab.font = [UIFont systemFontOfSize:13];
    statusLab.clipsToBounds = YES;
    [self addSubview:statusLab];
    
    
//    //物流UI
//    UIView *logisticsView = [[UIView alloc]initWithFrame:CGRectMake(0, orderView.bottom, self.width, HEADER_HEIGHT)];
//    //图标
//    RZCustomImageView *rightIcon=[[RZCustomImageView alloc] initWithFrame:CGRectMake(GAP, GAP, 20, 20)];
//    UIImage *image=BDFILEIMAGENAEM(@"Order/img_logisticsState");
//    [rightIcon setImage:image];
//    [logisticsView addSubview:rightIcon];
//    
//    //物流信息
//    [[self.logisticsDcit objectForKey:@"logisticsStatus"]  objectForKey:@"content"];
//    RZCustomLable *logisticsInfoLab=[[RZCustomLable alloc] init];
//    [logisticsInfoLab setFont:CustomFont(DEL_FontSize)];
//    [logisticsInfoLab setTextColor:[UtilCheck hexStringToColor:@"a1a1a1" alpha:1] ];
//    [logisticsView addSubview:logisticsInfoLab];
//    [logisticsInfoLab setFrame:CGRectMake(30 , GAP, self.width - rightIcon.width - 30 , 42)];
//    NSString *content = @"";
//    content = [[self.logisticsDcit objectForKey:@"logisticsStatus"]  objectForKey:@"content"];
//    if ([UtilCheck DataIsNuLL:content]) {
//        content = @"暂无物流信息";
//    }
//    logisticsInfoLab.text= content;
//
//    logisticsInfoLab.numberOfLines=0;
//    [logisticsInfoLab sizeToFit];
//    if (logisticsInfoLab.height < DEL_LABEL_HEIGHT) {
//        logisticsInfoLab.height = DEL_LABEL_HEIGHT;
//    }
//    
//    //日期
//    RZCustomLable *labDate=[[RZCustomLable alloc] init];
//    [labDate setFont:CustomFont(10)];
//    [labDate setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
//    [logisticsView addSubview:labDate];
//    [labDate setFrame:CGRectMake(30, logisticsInfoLab.bottom + GAP, self.width, DEL_LABEL_HEIGHT)];
//    labDate.text=[[self.logisticsDcit objectForKey:@"logisticsStatus"]  objectForKey:@"time"];
//    labDate.numberOfLines=1;
//    [labDate sizeToFit];
//    if (labDate.height < DEL_LABEL_HEIGHT) {
//        labDate.height = DEL_LABEL_HEIGHT;
//    }
//    
//    //进入图标
//    RZCustomImageView *accessImage = [[RZCustomImageView alloc]init];
//    [accessImage setImage:BDFILEIMAGENAEM(@"default/iconfont-jinru")];
//    [accessImage setFrame:CGRectMake(self.width - 20, HEADER_HEIGHT / 2 + 3.5 , 7, 14)];
//    [logisticsView addSubview:accessImage];
//    
//    [logisticsView setFrame:CGRectMake(0, orderView.bottom, self.width, GAP + logisticsInfoLab.height + labDate.height + GAP + GAP +GAP)];
//
//    
//    [self addSubview:logisticsView];
//    
//    logisticsView.userInteractionEnabled = YES;
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchLogisticsView)];
//    
//    [logisticsView addGestureRecognizer:tapGesture];
    
    //客户UI
//    UIView *receiverView = [[UIView alloc]initWithFrame:CGRectMake(0, orderView.bottom, self.width, HEADER_HEIGHT)];
//    
//    
//    //图标
//    RZCustomImageView *addressIcon=[[RZCustomImageView alloc] initWithFrame:CGRectMake(GAP, (HEADER_HEIGHT - 30)/2, 20, 20)];
//    UIImage *addressIconImage=BDFILEIMAGENAEM(@"Order/img_location");
//    [addressIcon setImage:addressIconImage];
//    addressIcon.size = addressIconImage.size;
//    [receiverView addSubview:addressIcon];
//    
//    //姓名和电话
//    RZCustomLable *nameAndTelLabel = [[RZCustomLable alloc] init];
//    [nameAndTelLabel setFont:CustomFont(DEL_FontSize)];
//    [receiverView addSubview:nameAndTelLabel];
//    [nameAndTelLabel setFrame:CGRectMake(30, GAP, (self.width-rightIcon.width)/2, DEL_LABEL_HEIGHT)];
//    [nameAndTelLabel setText:[NSString stringWithFormat:@"%@ %@",[self.receiverDict objectForKey:@"name"],[self.receiverDict objectForKey:@"tel"]]];
//    [nameAndTelLabel setTextColor:[UIColor darkGrayColor]];
//    if (nameAndTelLabel.height < DEL_LABEL_HEIGHT) {
//        nameAndTelLabel.height = DEL_LABEL_HEIGHT;
//    }
//    //是否加急
//    RZCustomLable *fastLab=[[RZCustomLable alloc] init];
//    [fastLab setFont:CustomFont(10)];
//    [fastLab setTextColor:[UIColor redColor] ];
//    [receiverView addSubview:fastLab];
//    [fastLab setFrame:CGRectMake(nameAndTelLabel.right, GAP + GAP, self.width/2 - rightIcon.width*2, DEL_LABEL_HEIGHT)];
//    NSString *htmlString = [self.receiverDict objectForKey:@"isUrgent"];
//    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
//    fastLab.attributedText = attrStr;
//    fastLab.textAlignment = NSTextAlignmentRight;
//    fastLab.numberOfLines=1;
////        [fastLab sizeToFit];
//    if (fastLab.height < DEL_LABEL_HEIGHT) {
//        fastLab.height = DEL_LABEL_HEIGHT;
//    }
//    
//    //地址
//    RZCustomLable *labAddress=[[RZCustomLable alloc] init];
//    [labAddress setFont:CustomFont(DEL_FontSize)];
//    [labAddress setTextColor:[UtilCheck hexStringToColor:@"a1a1a1" alpha:1] ];
//    [receiverView addSubview:labAddress];
//    [labAddress setFrame:CGRectMake(30 , nameAndTelLabel.bottom + GAP, self.width - rightIcon.width - 30 , 42)];
//    labAddress.text=[NSString stringWithFormat:@"%@",[self.receiverDict objectForKey:@"address"]];
//    labAddress.numberOfLines=0;
//    [labAddress sizeToFit];
//    if (labAddress.height < DEL_LABEL_HEIGHT) {
//        labAddress.height = DEL_LABEL_HEIGHT;
//    }
//    
//    [receiverView setFrame:CGRectMake(0, logisticsView.bottom, self.width, GAP + nameAndTelLabel.height + labAddress.height + GAP + GAP +GAP)];
//    
//    [self setFrame:CGRectMake(0, 0, self.width, HEADER_HEIGHT + receiverView.height +logisticsView.height)];
//    [self addSubview:receiverView];
    //客户UI
    UIView *receiverView = [[UIView alloc]initWithFrame:CGRectMake(0, orderView.bottom, self.width, HEADER_HEIGHT)];
    
    //图标
    RZCustomImageView *rightIcon=[[RZCustomImageView alloc] initWithFrame:CGRectMake(GAP*2, receiverView.height/2 - 10, 20, 20)];
    UIImage *image=BDFILEIMAGENAEM(@"Order/img_location");
    [rightIcon setImage:image];
    rightIcon.size = image.size;
    [receiverView addSubview:rightIcon];
    
    //姓名和电话
    RZCustomLable *nameAndTelLabel = [[RZCustomLable alloc] init];
    [nameAndTelLabel setFont:CustomFont(DEL_FontSize+1)];
    [receiverView addSubview:nameAndTelLabel];
    [nameAndTelLabel setFrame:CGRectMake(30 + GAP, GAP + 3, (self.width-rightIcon.width)/2, DEL_LABEL_HEIGHT)];
    [nameAndTelLabel setText:[NSString stringWithFormat:@"%@ %@",[self.receiverDict objectForKey:@"name"],[self.receiverDict objectForKey:@"tel"]]];
    [nameAndTelLabel setTextColor:[UIColor darkGrayColor]];
    if (nameAndTelLabel.height < DEL_LABEL_HEIGHT) {
        nameAndTelLabel.height = DEL_LABEL_HEIGHT;
    }
    
    //日期
//    RZCustomLable *labDate=[[RZCustomLable alloc] init];
//    [labDate setFont:CustomFont(11)];
//    [labDate setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
//    [receiverView addSubview:labDate];
//    [labDate setFrame:CGRectMake(self.width/2 , GAP + 3, self.width/2 - GAP*2, DEL_LABEL_HEIGHT)];
//    labDate.text=[self.receiverDict objectForKey:@"arriveTime"];
//    labDate.textAlignment = NSTextAlignmentRight;
//    labDate.numberOfLines=1;
//    if (labDate.height < DEL_LABEL_HEIGHT) {
//        labDate.height = DEL_LABEL_HEIGHT;
//    }
    
    //是否加急
    RZCustomLable *fastLab=[[RZCustomLable alloc] init];
    [fastLab setFont:CustomFont(10)];
    [fastLab setTextColor:[UIColor redColor] ];
    [receiverView addSubview:fastLab];
    [fastLab setFrame:CGRectMake(nameAndTelLabel.right, GAP + 3, self.width/2 - rightIcon.width*2, DEL_LABEL_HEIGHT)];
    NSString *htmlString = [self.receiverDict objectForKey:@"isUrgent"];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    fastLab.attributedText = attrStr;
    fastLab.textAlignment = NSTextAlignmentRight;
    fastLab.numberOfLines=1;
    //        [fastLab sizeToFit];
    if (fastLab.height < DEL_LABEL_HEIGHT) {
        fastLab.height = DEL_LABEL_HEIGHT;
    }
    
    //地址
    RZCustomLable *labAddress=[[RZCustomLable alloc] init];
    [labAddress setFont:CustomFont(DEL_FontSize)];
    [labAddress setTextColor:[UtilCheck hexStringToColor:@"545c66" alpha:1] ];
    [receiverView addSubview:labAddress];
    [labAddress setFrame:CGRectMake(30 + GAP , nameAndTelLabel.bottom + GAP - 4, self.width - rightIcon.width - 30 , 42)];
    labAddress.text=[NSString stringWithFormat:@"%@",[self.receiverDict objectForKey:@"address"]];
    labAddress.numberOfLines=0;
    [labAddress sizeToFit];
    if (labAddress.height < DEL_LABEL_HEIGHT) {
        labAddress.height = DEL_LABEL_HEIGHT;
    }
    
    [receiverView setFrame:CGRectMake(0, orderView.bottom, self.width, GAP + nameAndTelLabel.height + labAddress.height + GAP + GAP +GAP)];
    
    [self setFrame:CGRectMake(0, 0, self.width, orderView.bottom + receiverView.height)];
    [self addSubview:receiverView];
    //底部灰色的线
    RZCustomLable *lable=[[RZCustomLable alloc]initWithFrame:CGRectMake(0, self.height-0.5, self.width, 0.5)];
    [lable setBackgroundColor:[UtilCheck hexStringToColor:@"e5e5e5" alpha:1]];
    [self addSubview:lable];
  
    

}

- (void)touchLogisticsView{
    NSLog(@"点击了查看物流");
    
    
    CMyOrderCenterDetailCheck *vc = [[CMyOrderCenterDetailCheck alloc]init];
    DeliverModel *model = [[DeliverModel alloc]init];
    model.com =  self.logisticsDcit[@"logisticsInfo"][@"com"];
    model.code = self.logisticsDcit[@"logisticsInfo"][@"expressCode"];
    model.courierID = self.logisticsDcit[@"logisticsInfo"][@"courierID"];

    
    [vc setDeliver:model];
    [self.superview.viewController.navigationController pushViewController:vc animated:YES];
}
@end
